//
//  GameView.m
//  MTDBFROMJSON
//
//  Created by kingPC on 13-9-1.
//  Copyright (c) 2013年 kingPC. All rights reserved.
//

#import "GameView.h"
#import "Question.h"
#import "ViewController.h"

@implementation GameView

static int chooseBtnID=0;
@synthesize audioPlayer;

-(id)initWithQuestion:(Question*)q{
    self=[[[NSBundle mainBundle]loadNibNamed:@"GameView" owner:self options:nil]lastObject];
    if (self) {
        self.qu=q;
        currentTitlecount=1;
        titlecount=[self.qu.name length];
        [self setTitle];
        self.answer1.answerLabel.text=q.answer1.name;
        self.answer1.tag=[q.answer1.Aid intValue];
        [self setButtonSize:self.answer1];
        
        self.answer2.answerLabel.text=q.answer2.name;
        self.answer2.tag=[q.answer2.Aid intValue];
        [self setButtonSize:self.answer2];
        
        self.answer3.answerLabel.text=q.answer3.name;
        self.answer3.tag=[q.answer3.Aid intValue];
        [self setButtonSize:self.answer3];
        
        self.answer4.answerLabel.text=q.answer4.name;
        self.answer4.tag=[q.answer4.Aid intValue];
        [self setButtonSize:self.answer4];
        
        self.sureBtn.backgroundColor=[UIColor whiteColor];
        self.sureBtn.titleLabel.textColor=[UIColor blackColor];
        self.sureBtn.layer.cornerRadius = 8;
        self.sureBtn.layer.masksToBounds = YES;
        self.sureBtn.enabled=NO;
        
        
        if (![Global shareGlobal].fiftyfifty) {
            [self.fiftyfiftyBtn setImage:[UIImage imageNamed:@"5050NO"] forState:UIControlStateNormal];
            self.fiftyfiftyBtn.enabled=NO;
        }
    }
    return self;
}

-(void)viewLoadinit{
    rect1=self.answer1.answerLabel.frame;
    self.answer1.answerLabel.frame=CGRectMake(rect1.origin.x, rect1.origin.y+rect1.size.height/2, rect1.size.width, 0);
    rect2=self.answer2.answerLabel.frame;
    self.answer2.answerLabel.frame=CGRectMake(rect2.origin.x, rect2.origin.y+rect2.size.height/2, rect2.size.width, 0);
    rect3=self.answer3.answerLabel.frame;
    self.answer3.answerLabel.frame=CGRectMake(rect3.origin.x, rect3.origin.y+rect3.size.height/2, rect3.size.width, 0);
    rect4=self.answer4.answerLabel.frame;
    self.answer4.answerLabel.frame=CGRectMake(rect4.origin.x, rect4.origin.y+rect4.size.height/2, rect4.size.width, 0);
}

-(void)setTitle{
    if (currentTitlecount<titlecount) {
        self.question.text=[self.qu.name substringToIndex:currentTitlecount];
        [self performSelector:@selector(setTitle) withObject:nil afterDelay:0.2];
        currentTitlecount++;
    }else{
        [self step1];
    }
}


-(void)step1{
    [UIView animateWithDuration:0.5 animations:^(void){
        self.answer1.answerLabel.frame=rect1;
    }completion:^(BOOL finish){
        [self step2];
    }];
}
-(void)step2{
    [UIView animateWithDuration:0.5 animations:^(void){
        self.answer2.answerLabel.frame=rect2;
    }completion:^(BOOL finish){
        [self step3];
    }];
}
-(void)step3{
    [UIView animateWithDuration:0.5 animations:^(void){
        self.answer3.answerLabel.frame=rect3;
    }completion:^(BOOL finish){
        [self step4];
    }];
}
-(void)step4{
    [UIView animateWithDuration:0.5 animations:^(void){
        self.answer4.answerLabel.frame=rect4;
    }completion:^(BOOL finish){
        self.userInteractionEnabled=YES;
    }];
}



-(void)setButtonSize:(UIButton*)Button{
    Button.titleLabel.numberOfLines=0;
    [Button.titleLabel sizeToFit];
    
}

-(void)backGroundMusicRUN{
    NSString *musicPath = [[NSBundle mainBundle] pathForResource:@"Music_106"
                                                          ofType:@"mp3"];
    if (musicPath) {
        NSURL *musicURL = [NSURL fileURLWithPath:musicPath];
        self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL
                                                                  error:nil];
        [audioPlayer setDelegate:self];
    }
    
    if ([audioPlayer isPlaying]) {
        [audioPlayer stop];
    }
    else {
        [audioPlayer play];
    }
}

- (IBAction)re:(id)sender {
    [self.viewcontroller GmaeViewLoad];
}

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player
                      successfully:(BOOL)flag{
    [audioPlayer play];
}
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player{
    [audioPlayer play];
}

-(void)changeQuestion{
    [self.viewcontroller GmaeViewLoad];
}

- (IBAction)SureBtnPress:(id)sender {
    UIButton *answerBtn=(UIButton*)[self viewWithTag:[_qu.answerid intValue]];

    [answerBtn setBackgroundImage:[UIImage imageNamed:@"BtnRi"] forState:UIControlStateNormal];
    
    if (chooseBtnID==[_qu.answerid intValue]) {
//        [self.viewcontroller GmaeViewLoad];
        [self.viewcontroller NextView];
    }else{
        UIAlertView *alert=[[[UIAlertView alloc]initWithTitle:@"warming" message:@"Error" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]autorelease];
        [alert show];
    }
    
}

-(void)rightanswerB:(UIButton*)sender{
    
}
-(void)rightanswerG:(UIButton*)sender{
    
}

- (IBAction)FiftyFifty:(id)sender {
    int rs[]={_answer1.tag,_answer2.tag,_answer3.tag,_answer4.tag};
    for (int i=0; i<2;) {
        int r=rand()%4;
        int result=rs[r];
        if ([_qu.answerid intValue]!=result) {
            UIButton *bu=(UIButton*)[self viewWithTag:result];
            if (bu.enabled) {
                if (bu.tag==chooseBtnID) {
                    chooseBtnID=0;
                    [bu setBackgroundImage:[UIImage imageNamed:@"BtnR"] forState:UIControlStateNormal];
                }
                bu.enabled=NO;
                i++;
            }
        }
    }
    UIButton *B=(UIButton*)sender;
    [B setImage:[UIImage imageNamed:@"5050NO"] forState:UIControlStateNormal];
    B.enabled=NO;
    [Global shareGlobal].fiftyfifty=NO;
}

- (IBAction)AnswerBtnPress:(id)sender {
        self.sureBtn.enabled=YES;
    UIButton *b=(UIButton*)sender;
    chooseBtnID=b.tag;
    [self resetButton];
    [b setBackgroundImage:[UIImage imageNamed:@"BrnE"] forState:UIControlStateNormal];
    self.sureBtn.backgroundColor=[UIColor colorWithRed:251 green:0 blue:0 alpha:1];
    self.sureBtn.titleLabel.textColor=[UIColor whiteColor];
    b.titleLabel.textColor=[UIColor blackColor];
}

-(void)resetButton{
    if (_answer1.enabled) {
        [_answer1 setBackgroundImage:[UIImage imageNamed:@"BtnR"] forState:UIControlStateNormal];
        _answer1.titleLabel.textColor=[UIColor whiteColor];
    }
    if (_answer2.enabled) {
        [_answer2 setBackgroundImage:[UIImage imageNamed:@"BtnR"] forState:UIControlStateNormal];
        _answer2.titleLabel.textColor=[UIColor whiteColor];
    }
    if (_answer3.enabled) {
        [_answer3 setBackgroundImage:[UIImage imageNamed:@"BtnR"] forState:UIControlStateNormal];
        _answer3.titleLabel.textColor=[UIColor whiteColor];
    }
    if (_answer4.enabled) {
        [_answer4 setBackgroundImage:[UIImage imageNamed:@"BtnR"] forState:UIControlStateNormal];
        _answer4.titleLabel.textColor=[UIColor whiteColor];
    }
}

- (void)dealloc {
    [audioPlayer release];
    [_qu release];
    [_question release];
    [_answer1 release];
    [_answer2 release];
    [_answer3 release];
    [_answer4 release];
    [_sureBtn release];
    [_fiftyfiftyBtn release];
    [super dealloc];
}
@end
